import {
	AuthUtil,
	EditFormActionType,
	EditViewActionType,
	EditViewController,
	IEditFormAbility,
	IEditViewAbility,
	IEditViewControllerParams,
	IEditViewStore,
	IParam,
} from '@/core';

/**
 * 控制器（可查看可编辑样式）
 *
 * @export
 * @class ViewAndEditStyleController
 * @extends {EditViewController}
 */
export class ViewAndEditStyleController extends EditViewController {
	/**
	 * 仓库数据
	 *
	 * @protected
	 * @type {IViewAndEditStyleStore}
	 * @memberof ViewAndEditStyleController
	 */
	protected declare store: IViewAndEditStyleStore;
	/**
	 * 处理视图初始化
	 *
	 * @protected
	 * @param {IEditViewControllerParams<EditViewActionType, IEditViewAbility>} params
	 * @memberof ViewAndEditStyleController
	 */
	protected processViewInit(
		params: IEditViewControllerParams<EditViewActionType, IEditViewAbility>
	) {
		super.processViewInit(params);
		Object.assign(this.store, {
			editMode: false,
			toolbarItems_view: this.model.toolbarItems_view || [],
			toolbarItems_edit: this.model.toolbarItems_edit || [],
		});
	}

	/**
	 * 获取表单部件能力
	 *
	 * @protected
	 * @return {*}  {(IEditFormAbility | undefined)}
	 * @memberof ViewAndEditStyleController
	 */
	protected getMainCtrlAbility(): IEditFormAbility | undefined {
		const form = this.model.ctrls.find((ctrl: IParam) => {
			if (ctrl.controlType === 'FORM') {
				return this.store.editMode ? !ctrl.userTag : ctrl.userTag === 'VIEW';
			}
			return false;
		});
		if (form) {
			return this.getSubAbility<IEditFormAbility>(form.name);
		}
		return undefined;
	}

	/**
	 * 获取仓库数据
	 *
	 * @return {*}  {IViewAndEditStyleStore}
	 * @memberof ViewAndEditStyleController
	 */
	getStore(): IViewAndEditStyleStore {
		return this.store as IViewAndEditStyleStore;
	}

	/**
	 * 处理表单数据
	 *
	 * @protected
	 * @param {IParam[]} data
	 * @memberof ViewAndEditStyleController
	 */
	protected async handleFormData(data: IParam[]) {
		const arg = data[0];
		this.store.dataInfo = Object.is(arg.srfuf, '1')
			? this.model.majorPSAppDEField
				? arg[this.model.majorPSAppDEField.toLowerCase()]
				: arg.srfmajortext
			: App.ts("app.common.newlybuild","新建");
		AuthUtil.calcToolbarItemState(
			Object.is(arg.srfuf, '0'),
			this.store.editMode
				? this.store.toolbarItems_edit
				: this.store.toolbarItems_view
		);
		await this.calcToolbarItemAuthState(arg);
	}

	/**
	 * 计算工具栏权限状态
	 *
	 * @protected
	 * @param {IParam} data
	 * @memberof ViewAndEditStyleController
	 */
	protected async calcToolbarItemAuthState(data: IParam) {
		if (this.model.entityCodeName) {
			const UIService = await App.getUIService(
				this.model.entityCodeName,
				this.store.context
			);
			AuthUtil.calcToolbarItemAuthState(
				data,
				this.store.editMode
					? this.store.toolbarItems_edit
					: this.store.toolbarItems_view,
				UIService,
                this.store.viewCtx.view?.getModel()
			);
		}
	}

	/**
	 * 改变编辑状态
	 *
	 * @memberof ViewAndEditStyleController
	 */
	public changeEditMode() {
		this.store.editMode = !this.store.editMode;
		this.load();
	}
}

export interface IViewAndEditStyleStore extends IEditViewStore {
	/**
	 * 编辑模式
	 *
	 * @type {boolean}
	 * @memberof IViewAndEditStyleStore
	 */
	editMode: boolean;

	/**
	 * 查看态工具栏
	 *
	 * @type {IParam[]}
	 * @memberof IViewAndEditStyleStore
	 */
	toolbarItems_view: IParam[];

	/**
	 * 编辑态工具栏
	 *
	 * @type {IParam[]}
	 * @memberof IViewAndEditStyleStore
	 */
	toolbarItems_edit: IParam[];
}
